package com.qiqv.code.service.impl;

import com.qiqv.code.dao.AccountMapper;
import com.qiqv.code.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    //@Autowired
    //private TransactionTemplate transactionTemplate;

/*  使用编程式事务
  public void transfer(final Integer inputAccountId, final Integer outputAccountId, final Double total) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                accountMapper.plus(inputAccountId,total);
                int i = 1/0;
                accountMapper.minus(outputAccountId,total);
                System.out.println("交易完成...");
            }
        });

    }*/

/*  xml方式实现声明式事务
    public void transfer(final Integer inputAccountId, final Integer outputAccountId, final Double total) {
        accountMapper.plus(inputAccountId, total);
        int i = 1 / 0;
        accountMapper.minus(outputAccountId, total);
        System.out.println("交易完成...");
    }*/

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public void transfer(final Integer inputAccountId, final Integer outputAccountId, final Double total) {
        accountMapper.plus(inputAccountId, total);
        int i = 1 / 0;
        accountMapper.minus(outputAccountId, total);
        System.out.println("交易完成...");
    }
}
